package com.wiz.weixin.task;

import org.json.JSONException;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerContext;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.wiz.weixin.common.TencentConstant;
import com.wiz.weixin.manager.WxAccessManager;

public class TecentWxTokenTask extends QuartzJobBean implements TencentConstant{
	private transient final Logger log = LoggerFactory.getLogger(getClass());
	
	private WxAccessManager wxAccessManager;
	
	/**
     * 定时更新微信使用token，间隔1小时
	 * @throws Exception 
     */
	@Override
	protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
//		if(wxAccessManager == null){
//			SchedulerContext schCtx;
//			try {
//				schCtx = context.getScheduler().getContext();
//				//获取Spring中的上下文    
//				ApplicationContext appCtx = (ApplicationContext)schCtx.get("applicationContext");  
//				wxAccessManager = (WxAccessManager) appCtx.getBean("wxAccessManager");
//			} catch (SchedulerException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
//    	try {
//			if(wxAccessManager.resetWxToken()){
//				if(!wxAccessManager.resetJsApiTicket()){
//					log.error("reset Ticket fail,we must reset in 5 minutes");
//				}else{
//					wxAccessManager.resetJsApiTicket();
//				}
//			}else{
//				log.error("reset WxToken fail,we must reset in 5 minutes");
//				if(wxAccessManager.resetWxToken()){
//					if(!wxAccessManager.resetJsApiTicket()){
//						log.error("reset Ticket fail,we must reset in 5 minutes");
//					}else{
//						wxAccessManager.resetJsApiTicket();
//					}
//				}
//			}
//			log.info("reset token!!!!!!!!!  success token="+wxAccessManager.getTencent_wx_token()+"   ticket="+wxAccessManager.getTencent_wx_ticket());
//		} catch (JSONException e) {
//			try {
//				if(wxAccessManager.resetWxToken()){
//					if(!wxAccessManager.resetJsApiTicket()){
//						log.error("reset Ticket fail,we must reset in 5 minutes");
//					}else{
//						wxAccessManager.resetJsApiTicket();
//					}
//				}else{
//					log.error("reset WxToken fail,we must reset in 5 minutes");
//					if(wxAccessManager.resetWxToken()){
//						if(!wxAccessManager.resetJsApiTicket()){
//							log.error("reset Ticket fail,we must reset in 5 minutes");
//						}else{
//							wxAccessManager.resetJsApiTicket();
//						}
//					}
//				}
//				log.info("reset token!!!!!!!!!  success");
//			} catch (JSONException e1) {
//				log.error("reset WxToken fail,we must reset in 5 minutes");
//			}
//		}
    }
	
	
}
